<template>
  <basic-dialog
    width="60%"
    title="关联商品"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCancel"
  >
    <basic-form
      ref="formData"
      isBtnBarSingleRow
      :hasQueryPermission="false"
      :formColumns="formItems"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    >
      <BtnRender slot="extensionActions" :render="renderBtnsBar" />
    </basic-form>
    <SkusTable
      showSelectCol
      showPagination
      :tableHeight="480"
      :dataSource="TableInfo.list"
      :columns="TableInfo.columns"
      :loading="TableInfo.loading"
      :selectionChange="TableInfo.setSelection"
      :pageSizes="pageSizes"
      :pagination="pagination"
      :total="total"
      @pageChange="handlePageChange"
      @pageSizeSelect="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BtnRender from '@/components/JSXRender/BtnRender';
import SkusTable from '@/components/skusTable';
import skusPage from '@/mixins/commodity/skusPage';
import { getListPageBtnsBar } from '@/utils/getBtnsRender';
import { getInitFormData, TableInfo } from '@/utils/getUtils';
export default {
  mixins: [skusPage],
  components: {
    BasicDialog,
    BasicForm,
    BtnRender,
    SkusTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formItems: {
      type: Array,
      default: () => []
    },
    columns: {
      type: Array,
      default: () => []
    },
    loadData: {
      type: Function,
      default: () => {}
    }
  },
  data () {
    return {
      formData: getInitFormData(this.formItems),
      TableInfo: new TableInfo({ columns: this.columns }),
    }
  },
  methods: {
    // 渲染按钮操作栏
    renderBtnsBar (h) {
      const btnList = [
        { label: '批量关联', method: this.handleBatchAssociate },
        { label: '取消', method: this.handleCancel, type: 'default' }
      ]
      return getListPageBtnsBar(h, btnList)
    },
    // 获取表格数据
    async getTableData () {
      const configs = {
        formData: this.formData,
        paginationParams: this.paginationParams,
        loadingFn: this.TableInfo.setLoading
      }
      const res = await this.loadData(configs)
      const updateList = res?.retData?.results ? res?.retData?.results : res?.retData // 兼容接口返回数据格式（分页和不分页）
      this.TableInfo.setList(updateList ?? [])
      this.setTotalNum(res?.retData?.totalNum ?? 0)
    },
    // 批量关联
    handleBatchAssociate () {
      if (this.hasNoSelection) return this.$message.warning('请勾选商品后再进行关联')
      this.$emit('update', this.TableInfo.selection)
      this.resetDialogState()
      this.handleCancel()
    },
    // 取消
    handleCancel () {
      this.$emit('update:visible', false)
    },
    // 重置弹框状态
    resetDialogState () {
      this.$refs.formData.handleResetForm()
      this.TableInfo.setList([])
      this.resetPageInfo()
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
